package com.fulihui.usercore.core.repository.impl;


import com.fulihui.usercore.biz.conv.TagConv;
import com.fulihui.usercore.core.repository.UserTagRespitory;
import com.fulihui.usercore.dal.dataobj.TagDO;
import com.fulihui.usercore.dal.mapper.TagMapper;
import com.fulihui.usercore.dto.TagDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.UUID;

/**
 * Created by Willard.Hu on 2016/5/24.
 */
@Repository
public class UserTagRespitoryImpl implements UserTagRespitory {
    @Autowired
    private TagMapper tagMapper;

    @Override
    public void create(TagDTO tagDTO) {
        TagDO tagDO = TagConv.toDO(tagDTO);
        tagDO.setId(String.valueOf(UUID.randomUUID()));
        tagMapper.insert(tagDO);
    }

    @Override
    public boolean update(TagDTO tagDTO) {
        TagDO tagDO = TagConv.toDO(tagDTO);
        return tagMapper.update(tagDO);
    }

    @Override
    public boolean delete(String id) {
        return tagMapper.delete(id);
    }

    @Override
    public TagDTO queryByPK(String id) {
        TagDO tagDO = tagMapper.queryByPK(id);
        return TagConv.toDTO(tagDO);
    }

    @Override
    public List<TagDTO> queryByPKs(List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.emptyList();
        }
        List<TagDO> doList = tagMapper.queryByIds(ids);
        return TagConv.toList(doList);
    }

    @Override
    public List<TagDTO> queryAll() {
        List<TagDO> doList = tagMapper.queryAll();
        return TagConv.toList(doList);
    }

}
